﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLBH.DataModel;
using QLBH.Base;
using QLBH.Model.Controller;
using QLBH.ModelInterface;

namespace QLBH.FeatureModel
{
    public class MatHangItemModel:IMatHangItemModel
    {
        public Guid MatHangID { set; get; }
        public String MaMH { set; get; }
        public String TenMH { set; get; }
        public String DVT { set; get; }
        public decimal DonGia { set; get; }
        private int soluong;
        public int SoLuong 
        { 
            set
            {
                soluong = value;
                ThanhTien = DonGia * soluong - ChietKhau;
            }
            get
            {
                return soluong;
            }            
        }
        private decimal chietkhau;
        public decimal ChietKhau 
        { 
            set
            {
                chietkhau = value;
                ThanhTien = DonGia * soluong - ChietKhau;
            }
            get
            {
                return chietkhau;
            }
        }
        public decimal ThanhTien { set; get; }
        public void InitFromEntity(PHIEUMUAHANGCT entity)
        {
            MatHangID = entity.MHID;
            MATHANG mathang=MatHangController.GetInstance().FindBy(mh => mh.MHID == entity.MHID).FirstOrDefault();
            MaMH = mathang.MHCODE;
            TenMH = mathang.MHTEN;
            DonGia = (decimal)entity.DONGIA;
            SoLuong = (int)entity.SOLUONG;            
            DONVITINH dvtobj= DVTController.GetInstance().FindBy(dvt => dvt.DVTID == mathang.DVTID).FirstOrDefault();
            if (dvtobj!=null)
            {
                DVT = dvtobj.TENDVT;
            }
            ChietKhau = (decimal)entity.CHIETKHAU;          
        }

        public PHIEUMUAHANGCT ToEntity()
        {
            PHIEUMUAHANGCT entity = new PHIEUMUAHANGCT();
            entity.CHIETKHAU = (decimal)this.ChietKhau;
            entity.DONGIA = this.DonGia;
            entity.MHID = this.MatHangID;
            entity.SOLUONG = this.SoLuong;
            entity.THANHTIEN = this.ThanhTien;            
            return entity;
        }
        public void InitFromMatHangModel(MATHANG mh)
        {
            MatHangID = mh.MHID;            
            MaMH = mh.MHCODE;
            TenMH = mh.MHTEN;
            DonGia = (decimal)mh.GIANHAP;
            SoLuong = 0;            
            DONVITINH dvtobj = DVTController.GetInstance().FindBy(dvt => dvt.DVTID == mh.DVTID).FirstOrDefault();
            if (dvtobj != null)
            {
                DVT = dvtobj.TENDVT;
            }
            ChietKhau = (decimal)NCC_MatHangController.GetInstance().FindBy(nccmh => nccmh.MHID == mh.MHID).FirstOrDefault().CHIETKHAU;
        }
    }
}
